<template>
  <div>
    <!-- 防盗码申请-->
    <!-- 搜索 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" labelWidth="90" @filterChange="changeHandle" @resetChange="resetChange"></TopFilter>
    <!-- table -->
    <FilterTable
        ref="table"
        :columns="columns"
        :params="fetchParams"
        :fetchapi="fetchapi"
        :datakey="'records'"
        :onColumnsChange="(columns) => (this.columns = columns)"
        :onRowSelectChange="selectChange"
        :isSelectColumn="false"
        height="auto"
    >
    </FilterTable>

<!--    <Drawer :visible.sync="visibleAdd" :title="'防盗码申请'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">-->
<!--      <refundApply @close="closeAdd" @save="saveAdd"></refundApply>-->
<!--    </Drawer>-->
    <Drawer :visible.sync="editApplyType" :title="'车辆防盗码回复'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <editApply :rowData="formData"  @save="saveAdd" @close="detailcloseHandler" />
    </Drawer>

    <Drawer :visible.sync="detailVisible2" :title="'防盗码申请明细'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="detailcloseHandler2" />
    </Drawer>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :type="type" @close="ownercloseHandler" @save="delearSave" selectionType="multiple" />
    </BaseDialog>
  </div>
</template>
<script>
import { exportExcelFile } from '@/utils/index';
// import { getListByPage ,closeOrderRefund} from '@/api/omSaleP/BigOrderRefund/BigOrderRefundApply/index.js';
import { getListAll } from '@/api/plateNumber/car/index.js';
import { dictionary } from '@/utils/dictMixin';
import refundApply from './refundApply.vue';
import detailModel from './detailModel.vue';
import editApply from './editApply.vue';
import ownerSeacher from './ownerSeacher';
import moment from 'moment';
export default {
  mixins: [dictionary],
  components: {
    refundApply,
    detailModel,
    editApply,
    ownerSeacher,
  },
  data() {
    return {
      issearch: false,
      visible: false,
      emitVisible: false,
      visibleDrawer: false,
      showImport: false,
      infoId: null,
      fetchapi: getListAll,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商/claimDemage/BasicData/SpecialUserWarrantyMaintain/index
      rowData: {},
      dealerData:[],
      type:'',
      spId:'',
      visibleAdd: false,
      deailType:'',
      detailVisible: false,
      detailVisible2:false,
      editApplyType:false,
      formData:{},
    };
  },
  created() {
    let val = this.selectedRows;
    console.log("selectedRows==",this.selectedRows);
    console.log("fetchParams==",this.fetchParams);
  },
  methods: {
    editHandle2(row, type) {
      //编辑
      this.deailType = type;
      this.editApplyType = true;
      this.formData = row;
    },

    seacherFun(type) {
      this.type = type;
      this.issearch = true;
    },

    ownercloseHandler(val, type) {

      this.issearch = false;
    },

    delearSave(obj) {
      console.log('选择数据传递', obj);
      this.dealerData = obj;
      this.selectDealerData = [];
      obj.forEach(item => {
        //修改经销商传给后台的值
        this.selectDealerData.push(item.dealerCompanyCode);
      });
      console.log('this.selectDealerData=====', this.selectDealerData);
      this.issearch = false;
    },
    //重置
    resetChange(val) {
      this.changeHandle(val)
      this.selectDealerData= []
      this.fetchParams = Object.assign({}, this.fetchParams,{dealerNameStr:this.selectDealerData});
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let isOpen = true;
        row.map(x => {
          if (x.applyStatus != 60121002) {
            isOpen = false;
          }
        });
        this.isOpen = isOpen ? false : true;
        this.selectedList = row;
      } else {
        this.selectedList = [];
        this.isOpen = true;
        if (getAppId() == 'beijing') {
          this.isOpen = false;
        }
      }
    },

    add(){
      this.visibleAdd = true
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
    },
    detailcloseHandler() {
      this.detailVisible = false;
      this.editApplyType = false;
      if (this.deailType !== 'detail') {
        this.fetchParams = { ...this.fetchParams };
      }
    },
    detailcloseHandler2() {
      this.detailVisible2 = false;
      if (this.deailType !== 'detail') {
        this.fetchParams = { ...this.fetchParams };
      }
    },
    //明细页面
    async detailHandle(row, type) {
      //明细页面
      this.deailType = type;
      this.detailVisible2 = true;
      this.formData = row;
    },
    // 数据操作
    async editHandle(row, type) {
      if (type != 'cancel') {
        //编辑
        this.deailType = type;
        this.detailVisible = true;
        this.formData = row;
      } else {
        //删除
        await this.$confirm('是否确定作废?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
            .then(async () => {
              const res = await delectDate({ applyId: row.applyId });
              if (res.resultCode === 200) {
                this.$notify.success({ title: '提示信息', message: '成功' });
              }
              this.fetchParams = Object.assign({}, this.fetchParams);
            })
            .catch(() => {});
      }
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    emitFun(data,type) {
      this.type = type;
      this.rowData = data;
      this.emitVisible = true;
    },
    emitCloseHandle() {
      this.emitVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },

    saveAdd() {
      this.editApplyType = false;
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索获取数据
    changeHandle(val) {
      val.dealerNameStr = this.selectDealerData
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },


    // //导出
    // async downloadTemplate() {
    //   let [err, formData] = await this.$refs.TopFilterss.GET_FORM_DATA();
    //   console.log('formData',formData)
    //   if(!err){
    //     exportExcel(formData).then(res => {
    //       exportExcelFile(res.data, '特殊车辆包修期维护信息');
    //     });
    //   }
    // },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: 'VIN', // VIN
          fieldName: 'vin',
          placeholder: '请输入'
        },

        {
          type: 'SELECT',
          label: '申请状态',
          fieldName: 'status', //STATUS
          itemList: this.createDictList('2353').filter((item) => item.value == '23531002' || item.value == '23531003' || item.value == '23531004' ),
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'applyAtOne|applyAtTwo',  //APPLY_AT
          initialValue: '',
          format: 'yyyy-MM-dd',
          dateType: 'exactdaterange',
        },

        {
          type: 'INPUT',
          label: '申请经销商',
          fieldName: 'dealerName',
          placeholder: '请选择',
          // readonly: true,
          render: (p) => {
            return (
              <div>
                <el-select v-model={this.selectDealerData} disabled={true} multiple collapse-tags placeholder="请选择">
                  {this.dealerData.map((item) => (
                    <el-option key={item.dealerCode} label={item.dealerShortname} value={item.dealerCompanyCode} />
                  ))}
                </el-select>
                <multiuse-button style="position: absolute;right: 0;height:100%" icon="el-icon-search" onClick={() => this.issearch = true} />
              </div>
            );
          },
        },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          algin: 'center',
          fixed: 'left',
          render: (props, h) => {
            return (
                <div style="text-align:center">
                  <multiuse-button size="mini" type="text" disabled={props.row.status != 23531002} onClick={() => this.editHandle2(props.row)}>
                    回复
                  </multiuse-button>
                  <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
                    查看
                  </multiuse-button>

                </div>
            );
          },
        },
        {
          title: '申请状态',
          dataIndex: 'status', //STATUS
          sorter: true,
          filter: true,
          filterType: 'checkbox',
           filterItems: this.createDictList('2353'),
          // [
          //   { text: '待回复', value: 23531002 },
          //   { text: '已回复', value: 23531003 },
          //   { text: '已驳回', value: 23531004 },
          //
          // ],
          render: p => {
            return <span>{this.createDictText(p.row.status, '2353')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo', //APPLY_NO
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',  //VIN
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '防盗码',
          dataIndex: 'antitheftCode', // ANTITHEFT_CODE
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'model', //MODEL
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '申请时间',
          dataIndex: 'applyAt',  //APPLY_AT
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '申请经销商',
          dataIndex: 'dealerName',  //APPLY_AT
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '回复时间',
          dataIndex: 'replyAt',  //REPLY_AT
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '申请人',
          dataIndex: 'applyBy',  //APPLY_BY
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '回复人',
          dataIndex: 'replyBy', //REPLY_BY
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
<style lang="less" scoped></style>
